package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.config.CronTaskRegistrar;
import com.zygh.policeasset.config.SchedulingRunnable;
import com.zygh.policeasset.mapper.AuditFlowMapper;
import com.zygh.policeasset.mapper.EachTypeImagesMapper;
import com.zygh.policeasset.mapper.NoticeAfficheMapper;
import com.zygh.policeasset.mapper.NoticeAfficheUserMapper;
import com.zygh.policeasset.mapper.TaskMapper;
import com.zygh.policeasset.mapper.UploadFileMapper;
import com.zygh.policeasset.mapper.UserMapper;
import com.zygh.policeasset.pojo.EachTypeImages;
import com.zygh.policeasset.pojo.NoticeAffiche;
import com.zygh.policeasset.pojo.NoticeAfficheVO;
import com.zygh.policeasset.pojo.TableType;
import com.zygh.policeasset.pojo.Task;
import com.zygh.policeasset.pojo.UploadFile;
import com.zygh.policeasset.pojo.User;
import com.zygh.policeasset.utils.AuditUtil;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/NoticeAfficheService.class */
public class NoticeAfficheService {
    @Autowired
    NoticeAfficheMapper noticeAfficheMapper;
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    CronTaskRegistrar cronTaskRegistrar;
    @Autowired
    EachTypeImagesMapper eachTypeImagesMapper;
    @Autowired
    NoticeAfficheUserMapper noticeAfficheUserMapper;
    @Autowired
    UploadFileMapper uploadFileMapper;
    @Autowired
    AuditFlowMapper auditFlowMapper;
    @Autowired
    AuditFlowService auditFlowService;
    @Autowired
    UserMapper userMapper;

    public PageInfo findAll(int pageNum, int pageSize, NoticeAffiche noticeAffiche) {
        PageHelper.startPage(pageNum, pageSize);
        List<NoticeAfficheVO> list = this.noticeAfficheMapper.findAll(noticeAffiche);
        for (NoticeAfficheVO noticeAfficheVO : list) {
            noticeAfficheVO.setCheckStatus(this.noticeAfficheUserMapper.selectBynoticeAfficheId(noticeAfficheVO.getId(), null));
        }
        return new PageInfo(list);
    }

    public PageInfo findAllByUserId(int pageNum, int pageSize, Integer userId, Integer type, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        List<NoticeAfficheVO> list = this.noticeAfficheMapper.findAllByUserId(2, userId, type, status);
        for (NoticeAfficheVO noticeAfficheVO : list) {
            List<EachTypeImages> eachTypeImagesList = this.eachTypeImagesMapper.findbyTypeIdAndType(noticeAfficheVO.getId(), "notice");
            if (eachTypeImagesList.size() > 0) {
                noticeAfficheVO.setImagesUrl(eachTypeImagesList.get(0).getConnect());
            }
        }
        for (NoticeAfficheVO noticeAfficheVO2 : list) {
            noticeAfficheVO2.setCheckStatus(this.noticeAfficheUserMapper.selectBynoticeAfficheId(noticeAfficheVO2.getId(), userId));
        }
        return new PageInfo(list);
    }

    public NoticeAfficheVO SelectById(Integer id) {
        NoticeAfficheVO noticeAfficheVO = this.noticeAfficheMapper.SelectById(id);
        noticeAfficheVO.setUsersName(this.userMapper.findByIds(noticeAfficheVO.getUsers().split(",")));
        return noticeAfficheVO;
    }

    @Transactional(rollbackFor = {Exception.class})
    public CommonResult add(NoticeAffiche noticeAffiche) {
        try {
            String now = DateUtil.now();
            String flowNo = AuditUtil.getFlowNo(now);
            noticeAffiche.setFlowNo(flowNo);
            noticeAffiche.setSend(0);
            noticeAffiche.setCreatTime(now);
            if (noticeAffiche.getType().intValue() == 1) {
                List<User> findAllUser = this.userMapper.findAllUser(null, null);
                String users = "";
                for (int i = 0; i < findAllUser.size(); i++) {
                    users = users + "," + findAllUser.get(i).getId();
                }
                noticeAffiche.setUsers(users);
            }
            this.noticeAfficheMapper.add(noticeAffiche);
            this.auditFlowService.add(noticeAffiche.getUserId(), flowNo, now, TableType.NOTICEAFFICHE.getName(), TableType.NOTICEAFFICHE.getType(), null, null);
            if (noticeAffiche.getType().intValue() == 1) {
                if (noticeAffiche.getUpoladEachTypeImages() != null) {
                    this.eachTypeImagesMapper.add(noticeAffiche.getId(), noticeAffiche.getUpoladEachTypeImages().getContent(), DateUtil.now(), noticeAffiche.getUpoladEachTypeImages().getType());
                }
            } else if (noticeAffiche.getUploadFileList().size() > 0) {
                List<UploadFile> uploadFileList = noticeAffiche.getUploadFileList();
                for (int i2 = 0; i2 < uploadFileList.size(); i2++) {
                    uploadFileList.get(i2).setProjectId(noticeAffiche.getId().intValue());
                    uploadFileList.get(i2).setProjectName("notice");
                    uploadFileList.get(i2).setCreateDate(DateUtil.now());
                    this.uploadFileMapper.add(uploadFileList.get(i2));
                }
            }
            Task task = new Task();
            task.setBeanName("NoticeAfficheTask");
            task.setMethoDname("noticeAffiche");
            task.setComment("定时发送公告或通知");
            task.setCycle(0);
            task.setNoticeAfficheId(noticeAffiche.getId());
            task.setRate(0);
            if (!StringUtils.isEmpty(noticeAffiche.getSendTime())) {
                task.setTaskTime(timeToCron(noticeAffiche.getSendTime()));
            } else {
                task.setTaskTime(timeToCron(DateUtil.now()));
            }
            this.taskMapper.add(task);
            this.cronTaskRegistrar.addCronTask(new SchedulingRunnable(task.getBeanName(), task.getMethoDname(), JSONObject.fromObject(task).toString()), task.getTaskTime());
            return new CommonResult().success();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    public Integer updateRead(Integer noticeAfficheId, Integer userId) {
        return this.noticeAfficheUserMapper.updateRead(noticeAfficheId, userId, 1);
    }

    @Transactional(rollbackFor = {Exception.class})
    public CommonResult update(NoticeAffiche noticeAffiche) {
        try {
            Task task = this.taskMapper.queryTaskByNoticeAfficheId(noticeAffiche.getId());
            this.cronTaskRegistrar.removeCronTask(new SchedulingRunnable(task.getBeanName(), task.getMethoDname(), JSONObject.fromObject(task).toString()));
            this.noticeAfficheMapper.update(noticeAffiche);
            if (!StringUtils.isEmpty(noticeAffiche.getSendTime())) {
                this.taskMapper.updateTask(null, task.getId(), timeToCron(noticeAffiche.getSendTime()));
                task.setTaskTime(timeToCron(noticeAffiche.getSendTime()));
            } else {
                this.taskMapper.updateTask(null, task.getId(), timeToCron(DateUtil.now()));
                task.setTaskTime(timeToCron(timeToCron(DateUtil.now())));
            }
            this.cronTaskRegistrar.addCronTask(new SchedulingRunnable(task.getBeanName(), task.getMethoDname(), JSONObject.fromObject(task).toString()), timeToCron(noticeAffiche.getSendTime()));
            this.noticeAfficheMapper.update(noticeAffiche);
            return new CommonResult().success();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    public CommonResult delete(Integer id) {
        try {
            Task task = this.taskMapper.queryTaskByNoticeAfficheId(id);
            this.cronTaskRegistrar.removeCronTask(new SchedulingRunnable(task.getBeanName(), task.getMethoDname(), JSONObject.fromObject(task).toString()));
            this.taskMapper.delete(id);
            this.noticeAfficheMapper.delete(id.intValue());
            return new CommonResult().success();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    public static String timeToCron(String time) {
        LocalDateTime s = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        StringBuffer code = new StringBuffer();
        if (s.getSecond() < 10) {
            code.append("0");
            code.append(s.getSecond());
        } else {
            code.append(s.getSecond());
        }
        code.append(" ");
        if (s.getMinute() < 10) {
            code.append("0");
            code.append(s.getMinute());
        } else {
            code.append(s.getMinute());
        }
        code.append(" ");
        if (s.getHour() < 10) {
            code.append("0");
            code.append(s.getHour());
        } else {
            code.append(s.getHour());
        }
        code.append(" ");
        if (s.getDayOfMonth() < 10) {
            code.append("0");
            code.append(s.getDayOfMonth());
        } else {
            code.append(s.getDayOfMonth());
        }
        code.append(" ");
        if (s.getMonthValue() < 10) {
            code.append("0");
            code.append(s.getMonthValue());
        } else {
            code.append(s.getMonthValue());
        }
        code.append(" ?");
        return code.toString();
    }
}